// <script type="application/javascript" src="/c19032/semiautomatic/stomp.js" ></script>
// <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
layui.config({
    base: CXL.ctxPath + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'table', 'laydate', 'form', 'layer', 'element'], function () {
    var admin = layui.admin;
    var table = layui.table;
    var laydate = layui.laydate;
    var $ = layui.$;
    var form = layui.form;
    let element = layui.element;
    let router = layui.router();
    var order_num = 1;

    // <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
    //开始点和结束点
    var selectedAction = [];

    initTempTable();


    //清空缓存表
    function initTempTable() {
        $.ajax({
            type: "get",
            url: CXL.ctxPath + "/semiautomatic/combinationtask/tempTable/truncatetemptable",
            success: function (res) {
                if (res.success) {
                    CXL.success(res.msg);
                    // layer.msg(res.msg);
                    //表格删除
                    // table.reload();
                } else {
                    CXL.warn(res.msg);
                    // layer.msg(res.msg);
                }
            }
        });

    }

    /**
     * 表格参数
     */
    var Page = {
        tableId: 'actionTable',
        url: '/boot/semiautomaticway/datagrid',
        toolbar: '#table-toolbar',
        // where:{field:'createDate', order:'desc'},
        initColumn: function () {
            return [[
                // {type: 'numbers'},
                {field: 'id', title: 'ID', width: 80}
                , {field: 'name', title: '动作名称', width: 180}
                , {field: 'craneId', title: '天车ID', width: 100}
                , {
                    field: 'workMode', title: '工作模式', width: 100, templet: function (d) {
                        if (d.workMode == "WORK_TYPE_POUR") {
                            res = "倒料"
                        } else if (d.workMode == "dump") {
                            res = "放料"
                        } else if (d.workMode == "WORK_TYPE_LOAD_MATERIAL_NORMAL") {
                            res = "上料"
                        } else {
                            res = "移动"
                        }
                        return res;
                    }
                }
                , {field: 'catchNumber', title: '抓取次数', width: 180}
                , {field: 'frequency', title: '运行次数', width: 180}
                , {field: 'right', title: '操作', toolbar: '#table-toolbar', width: 115}
            ]];
        },
        deleteUrl: "/boot/semiautomaticway/delete"

    };

    /**
     * 动作表格渲染
     */

        // var queryData = {};
        // 查询字段
        // queryData['craneId'] = 2;
    var tableIns = table.render({
            elem: '#' + Page.tableId
            , url: Page.url
            // , height: 'full-140'
            , page: true
            , limit: 9
            , autoSort: false
            , cols: Page.initColumn()
        });
    /**
     * 表格的单击事件
     */
    Page.clickAction = function (obj) {

    }

    /**
     * 表格的双击事件
     */
    Page.doubleClickAction = function (obj) {

    }
    /**
     * 删除表格数据
     */
    Page.deleteAction = function (obj) {
        parent.layer.confirm('真的确定要删除么', function (index) {

            //提交 Ajax进行后端删除
            $.ajax({
                type: "post",
                url: Page.deleteUrl,
                data: {id: obj.data.id},
                success: function (res) {
                    if (res.success) {
                        CXL.success(res.msg);
                        // layer.msg(res.msg);
                        //表格删除
                        obj.del();
                        Page.searchAction();
                        // table.reload();
                    } else {
                        CXL.warn(res.msg);
                        // layer.msg(res.msg);
                    }
                }
            });

            layer.close(index);
        });
    }

    //加入缓存列表
    Page.addAction = function (data) {
        let wayId = data.id;
        let actionName = data.name;
        let workFrequency = data.frequency;
        console.log(actionName);
        let addData = {
            wayId: wayId,
            actionName: actionName,
            workFrequency: workFrequency,
            orderNum: order_num
        }
        console.log(addData);
        $.ajax({
            type: "post",
            url: CXL.ctxPath + "/semiautomatic/combinationtask/postTemp",
            data: addData,
            dataType: "json",
            success: function (res) {
                if (res.success) {
                    console.log(res.msg);
                    CXL.success(res.msg);
                    tempTableIns.reload();
                    order_num++;
                } else {
                    CXL.warn(res.msg);
                    // layer.msg(res.msg);
                }
            },
            fail: function () {
                CXL.error("网络错误！");
            }
        });


    }
    /**
     * 监听表格工具事件
     */
    // table.on('tool(' + Page.table-toolbar + ')', function (obj) {
    table.on('tool(actionTableRender)', function (obj) {
        //获得当前行数据
        var data = obj.data;
        //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var layEvent = obj.event;


        if (layEvent === 'delete') {
            Page.deleteAction(obj);
        }
        //删除操作
        else if (layEvent === 'load') {
            Page.loadAction(obj);
        }
//添加缓存动作
        else if (layEvent === 'addAction') {
            Page.addAction(data);
        }
    });

//删除数组指定元素
    function deleteFromArrayByElement(array, element) {
        array.forEach(function (item, index, arr) {
            if (item.name == element.name) {
                arr.splice(index, 1);
            }
        });
    }

//选择点
    function addAction(item, arr) {
        arr.push(item);
        refreshIsSelectedTable();
    }

    //刷新已选择动作
    function refreshIsSelectedTable() {

    }

    //刷新已经选择表


    /**
     * 表格参数
     */
    var TempPage = {
        tableId: 'combination-temp-table',
        url: '/boot/semiautomatic/combinationtask/tempTable/datagrid',
        // where:{field:'createDate', order:'desc'},
        initColumn: function () {
            return [[
                // {type: 'numbers'},
                {field: 'id', title: 'ID', width: 80, hide: true}
                , {field: 'wayId', title: '动作编号', width: 100}
                , {field: 'actionName', title: '动作名称', width: 120}
                , {field: 'workFrequency', title: '次数', width: 80}
                , {field: 'actionOrder', title: '次序', width: 100}
                , {field: 'right', title: '操作', toolbar: '#temp-table-toolbar', width: 115}
            ]];
        },
        deleteUrl: "/boot/semiautomatic/combinationtask/tempTable/delete"

    };

    /**
     * 动作表格渲染
     */

        // var queryData = {};
        // 查询字段
        // queryData['craneId'] = 2;
    var tempTableIns = table.render({
            elem: '#' + TempPage.tableId
            , url: TempPage.url
            // , height: 'full-140'
            , Page: true
            , limit: 5
            , autoSort: false
            , cols: TempPage.initColumn()
        });
    /**
     * 表格的单击事件
     */
    TempPage.clickAction = function (obj) {

    }

    /**
     * 表格的双击事件
     */
    TempPage.doubleClickAction = function (obj) {

    }
    /**
     * 删除表格数据
     */
    TempPage.deleteAction = function (obj) {
        parent.layer.confirm('真的确定要删除么', function (index) {

            //提交 Ajax进行后端删除
            $.ajax({
                type: "post",
                url: TempPage.deleteUrl,
                data: {id: obj.data.id},
                success: function (res) {
                    if (res.success) {
                        CXL.success(res.msg);
                        order_num--;
                        // layer.msg(res.msg);
                        //表格删除
                        obj.del();
                        tempTableIns.reload();
                        // table.reload();
                    } else {
                        CXL.warn(res.msg);
                        // layer.msg(res.msg);
                    }
                }
            });

            layer.close(index);
        });
    }

    //保存组合任务
    TempPage.saveAction = function () {
        let name = $("#combinationName").val()
        //提交 Ajax进行后端删除
        $.ajax({
            type: "post",
            url: CXL.ctxPath + "/semiautomatic/combinationtask/tempTable/savecombinationtask",
            data: {name: name},
            success: function (res) {
                if (res.success) {
                    CXL.success(res.msg);
                    initTempTable();
                    tempTableIns.reload();
                    // table.reload();
                } else {
                    CXL.warn(res.msg);
                    // layer.msg(res.msg);
                }
            }
        });
    }

    /**
     * 监听表格工具事件
     */
    // table.on('tool(' + TempPage.table-toolbar + ')', function (obj) {
    table.on('tool(combination-temp-table-filter)', function (obj) {
        //获得当前行数据
        var data = obj.data;
        //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var layEvent = obj.event;


        if (layEvent === 'delete') {
            TempPage.deleteAction(obj);
        }
        //删除操作
        else if (layEvent === 'load') {
            TempPage.loadAction(obj);
        }
//添加缓存动作
        else if (layEvent === 'addAction') {
            TempPage.addAction(data);
        }
    });

    /**
     * 搜索按钮事件
     */
    $('#btnSave').click(function () {
        TempPage.saveAction();
    });


    /**
     * 表格参数
     */
    var CombinationPage = {
        tableId: 'combinationTable',
        url: CXL.ctxPath + '/semiautomatic/combinationtask/combinationTable/datagrid',
        initColumn: function () {
            return [[
                {type: 'numbers'}
                , {field: 'id', title: 'ID', hide: true, width: 80}
                , {field: 'name', title: '任务名称', width: 120}
                , {fixed: 'right', title: '操作', toolbar: '#combination-table-toolbar', width: 170}
            ]];
        },
        deleteUrl: CXL.ctxPath + "/semiautomatic/combinationtask/combinationTable/delete",
    };


    /**
     * 页面查询
     */
    CombinationPage.searchAction = function () {
        var queryData = {};
        //查询字段
        console.log($("#timeBegin").val());
        console.log($("#timeEnd").val());
        queryData['workType'] = $("#tasktypeinput").val();
        queryData['craneId'] = $("#craneidinput").val();
        queryData['beginTime'] = $("#timeBegin").val();
        queryData['endTime'] = $("#timeEnd").val();

        // //排序字段
        // queryData['field'] = 'operatorTime';
        // queryData['order'] = 'desc';

        table.reload(CombinationPage.tableId, {where: queryData});
    }


    /**
     * 页面查询条件的重置
     */
    CombinationPage.resetAction = function () {
        let xdate = new Date("2019-04-30");
        console.log(xdate);
        $("#tasktypeinput").val("");
        $("#craneidinput").val(0);
        $("#timeBegin").val(xdate);
        $("#timeEnd").val(new Date());
        CombinationPage.searchAction();
    }


    /**
     * 表格的单击事件
     */
    CombinationPage.clickAction = function (obj) {

    }

    /**
     * 表格的双击事件
     */
    CombinationPage.doubleClickAction = function (obj) {

    }


    /**
     * 查看
     */
    CombinationPage.openDetail = function (data) {
        var data = data.data;
        var layer = layui.layer;
        layer.open({
            type: 1,
            content: $('#openShowBox'),
            area: ['700px', '800px'], //宽高
            btn: ["确定"],
            success: function (index, layero) {

                table.render({
                    elem: '#openShowTaskDetails'
                    , height: 'full-140'
                    , limit: 30
                    , autoSort: false
                    , url: CombinationPage.url + "/" + data.id
                    , Page: true
                    , cols: [[ //表头
                        {field: 'id', title: 'ID', width: 80}
                        , {field: 'name', title: '动作名称', width: 100}
                        , {field: 'craneId', title: '天车ID', width: 100}
                        , {
                            field: 'workMode', title: '工作模式', width: 180, templet: function (d) {
                                if (d.workType == "WORK_TYPE_LOAD_MATERIAL_NORMAL") {
                                    res = "上料"
                                } else if (d.workType == "WORK_TYPE_POUR") {
                                    res = "放料"
                                } else if (d.workType == "WORK_TYPE_MOVE") {
                                    res = "移动"
                                } else {
                                    res = "放料"
                                }
                                return res;
                            }
                        }
                        , {field: 'catchNumber', title: '抓取次数', width: 100}
                        , {field: 'frequency', title: '运行次数', width: 180}
                    ]]
                });

            }
        });
    }

    //删除动作组合任务
    CombinationPage.deleteAction = function (obj) {
        parent.layer.confirm('真的确定要删除么', function (index) {

            //提交 Ajax进行后端删除
            $.ajax({
                type: "post",
                url: CombinationPage.deleteUrl,
                data: {id: obj.data.id},
                success: function (res) {
                    if (res.success) {
                        CXL.success(res.msg);
                        order_num--;
                        // layer.msg(res.msg);
                        //表格删除
                        obj.del();
                        combinationTable.reload();
                        // table.reload();
                    } else {
                        CXL.warn(res.msg);
                        // layer.msg(res.msg);
                    }
                }
            });

            layer.close(index);
        });

    }


    /**
     * 表格渲染
     */
    var combinationTable = table.render({
        elem: '#' + CombinationPage.tableId
        , url: CombinationPage.url
        , height: 'full-140'
        , page: true
        , limit: 30
        , autoSort: false
        , toolbar: CombinationPage.toolbar
        , where: CombinationPage.where
        , defaultToolbar: []

        , cols: CombinationPage.initColumn()
    });


    /**
     * 监听表格工具事件
     */
    table.on('tool(combinationTaskFilter)', function (obj) {
        //获得当前行数据
        var data = obj.data;
        //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var layEvent = obj.event;

        //查看操作
        if (layEvent === 'detail') {
            CombinationPage.openDetail(obj);
        }
        //删除操作
        else if (layEvent === 'delete') {
            CombinationPage.deleteAction(obj);
        }
        //编辑操作
        else if (layEvent === 'view') {
            CombinationPage.openEdit(data);
        } else if (layEvent === 'start') {
            CombinationPage.startTask(data);
        } else if (layEvent === 'createWork') {
            CombinationPage.createWork(data);
        }
    });
    $(".layui-btn.cd").innerHTML = "sadasd";
});